package com.lzx.iteam;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import com.lzx.iteam.adapter.AlbumGridViewAdapter;
import com.lzx.iteam.selectimage.Bimp;
import com.lzx.iteam.selectimage.PublicWay;
import com.lzx.iteam.selectimage.Res;
import com.lzx.iteam.util.Constants;
import io.rong.imageloader.core.listener.PauseOnScrollListener;

import java.util.ArrayList;

import static com.lzx.iteam.util.ImageLoaderInterface.imageLoader;

/**
 * 这个是显示一个文件夹里面的所有图片时的界面
 *
 * @author king
 * @QQ:595163260
 * @version 2014年10月18日  下午11:49:10
 */
public class ShowImagesActivity extends FragmentActivity {
	private GridView gridView;
	private ProgressBar progressBar;
	private AlbumGridViewAdapter gridImageAdapter;
	// 完成按钮
	private Button okButton;
	// 预览按钮
	private Button preButton;
	// 返回按钮
	private TextView back;
	// 取消按钮
	private TextView cancel;
	// 标题
	private TextView headTitle;
	private Intent intent;
	private Context mContext;
	private int mImageNumber = 9;

	public static ArrayList<String> dataList = new ArrayList<String>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(Res.getLayoutID("plugin_camera_show_all_photo"));
		//PublicWay.activityList.add(this);
		mContext = this;
		back = (TextView) findViewById(Res.getWidgetID("showallphoto_back"));
		cancel = (TextView) findViewById(Res.getWidgetID("showallphoto_cancel"));
		okButton = (Button) findViewById(Res.getWidgetID("showallphoto_ok_button"));
		headTitle = (TextView) findViewById(Res.getWidgetID("showallphoto_headtitle"));
		this.intent = getIntent();
		String folderName = intent.getStringExtra("folderName");
		mImageNumber = intent.getIntExtra("imageNumber",9);
		if (folderName.length() > 8) {
			folderName = folderName.substring(0, 9) + "...";
		}
		headTitle.setText(folderName);
		cancel.setOnClickListener(new CancelListener());
		back.setOnClickListener(new BackListener(intent));
		init();
		initListener();
		isShowOkBt();
	}

	@Override
	protected void onResume() {
		super.onResume();
		applyScrollListener();

	}

	/**
	 * 滑动的过程中不加载图片，滑动停止开始加载
	 */
	private void applyScrollListener() {
		gridView.setOnScrollListener(new PauseOnScrollListener(imageLoader,  false, true));
	}

	private class PreviewListener implements OnClickListener {
		public void onClick(View v) {
			/*if (Bimp.tempSelectBitmap.size() > 0) {
				intent.putExtra("position", "2");
				intent.setClass(ShowAllPhoto.this, GalleryActivity.class);
				startActivity(intent);
			}*/
		}

	}

	private class BackListener implements OnClickListener {// 返回按钮监听
		Intent intent;

		public BackListener(Intent intent) {
			this.intent = intent;
		}

		public void onClick(View v) {
			finish();
		}

	}

	private class CancelListener implements OnClickListener {
	// 取消按钮的监听
		public void onClick(View v) {
			//清空选择的图片
			Bimp.tempSelectBitmap.clear();
			finish();
		}
	}

	private void init() {
		progressBar = (ProgressBar) findViewById(Res.getWidgetID("showallphoto_progressbar"));
		progressBar.setVisibility(View.GONE);
		gridView = (GridView) findViewById(Res.getWidgetID("showallphoto_myGrid"));
		gridImageAdapter = new AlbumGridViewAdapter(this,dataList,
				Bimp.tempSelectBitmap);
		gridView.setAdapter(gridImageAdapter);
		preButton = (Button) findViewById(Res.getWidgetID("showallphoto_preview"));
		okButton = (Button) findViewById(Res.getWidgetID("showallphoto_ok_button"));
	}

	private void initListener() {

		gridImageAdapter
				.setOnItemClickListener(new AlbumGridViewAdapter.OnItemClickListener() {
					public void onItemClick(final ToggleButton toggleButton,
							int position, boolean isChecked,
											ImageView button) {
						if (Bimp.tempSelectBitmap.size() >= mImageNumber && isChecked) {
							//button.setVisibility(View.GONE);
							button.setImageDrawable(mContext.getResources().getDrawable(R.drawable.img_uncheck));
							toggleButton.setChecked(false);
							Toast.makeText(ShowImagesActivity.this, Res.getString("only_choose_num"), 200)
									.show();
							return;
						}

						if (isChecked) {
							//button.setVisibility(View.VISIBLE);
							button.setImageDrawable(mContext.getResources().getDrawable(R.drawable.img_checked));
							Bimp.tempSelectBitmap.add(dataList.get(position));
							okButton.setText(Res.getString("finish")+"(" + Bimp.tempSelectBitmap.size()
									+ "/" + mImageNumber + ")");
						} else {
							//button.setVisibility(View.GONE);
							button.setImageDrawable(mContext.getResources().getDrawable(R.drawable.img_uncheck));
							Bimp.tempSelectBitmap.remove(dataList.get(position));
							okButton.setText(Res.getString("finish")+"(" + Bimp.tempSelectBitmap.size() + "/" + mImageNumber + ")");
						}
						isShowOkBt();
					}
				});

		okButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				okButton.setClickable(false);
				ImageFileActivity.instance.finish();
				finish();

			}
		});

		preButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ShowImagesActivity.this, ImagePagerActivity.class);
				intent.putExtra(Constants.IMAGE_ORIGIN, Constants.IMAGE_ORIGIN_LOCAL_ARRAY);
				intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, Bimp.tempSelectBitmap);
				intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, 0);
				startActivity(intent);

			}
		});


	}

	public void isShowOkBt() {
		if (Bimp.tempSelectBitmap.size() > 0) {
			okButton.setText(Res.getString("finish")+"(" + Bimp.tempSelectBitmap.size() + "/"+mImageNumber+")");
			okButton.setPressed(true);
			okButton.setClickable(true);
			okButton.setTextColor(Color.WHITE);
			preButton.setText("预览(" + Bimp.tempSelectBitmap.size()+")");
			preButton.setPressed(true);
			preButton.setClickable(true);
			preButton.setTextColor(Color.WHITE);

		} else {
			okButton.setText(Res.getString("finish")+"(" + Bimp.tempSelectBitmap.size() + "/"+mImageNumber+")");
			okButton.setPressed(false);
			okButton.setClickable(false);
			okButton.setTextColor(Color.parseColor("#E1E0DE"));
			preButton.setText("预览(" + Bimp.tempSelectBitmap.size()+")");
			preButton.setPressed(true);
			preButton.setClickable(true);
			preButton.setTextColor(getResources().getColor(R.color.gray_third));
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.finish();
		}

		return false;

	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		isShowOkBt();
		super.onRestart();
	}

}
